/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package biofilter.data.Sequence;

import biofilter.data.Sequence.BioSequence;
import biofilter.util.*;

/**
 *
 * @author Daniel Quest
 * @date   Jan 11 2009
 */
public class NASequence extends BioSequence {

    public NASequence(){
        super.init();
    }

    public NASequence(String s){
        super(s);
    }

    public NASequence(String s, char strand){
        super(s);
        sp.setStrand(strand);
    }

    public boolean legalEMBLSequenceLine(String line){
        if (line.matches("^\\s{5}.*\\d+")){
            return true;  // we know this is not completely true... but for now we will use it until time to revise.
        }else{//Illigal because line does not contain a number at the end.
            return false;
        }
        //return false;
    }

    /**
        A --> adenosine           M --> A C (amino)
        C --> cytidine            S --> G C (strong)
        G --> guanine             W --> A T (weak)
        T --> thymidine           B --> G T C
        U --> uridine             D --> G A T
        R --> G A (purine)        H --> A C T
        Y --> T C (pyrimidine)    V --> G C A
        K --> G T (keto)          N --> A G C T (any)
                                  -  gap of indeterminate length
     */
    public boolean valid(String s){
        if(s.matches("^[ACGTURYKMSWBDHVNacgturykmswbdhvn\\-]+"))
            return true;
        if(s.matches("^[\\sACGTURYKMSWBDHVNacgturykmswbdhvn\\-]+\\d+"))
            return true;
        return false;
    }

    public boolean valid(char c){
        if(c == 'A' || c == 'a') return true;
        if(c == 'C' || c == 'c') return true;
        if(c == 'G' || c == 'g') return true;
        if(c == 'T' || c == 't') return true;
        if(c == 'N' || c == 'n') return true;
        if(c == 'U' || c == 'u') return true;
        if(c == 'R' || c == 'r') return true;
        if(c == 'Y' || c == 'y') return true;
        if(c == 'K' || c == 'k') return true;
        if(c == 'M' || c == 'm') return true;
        if(c == 'S' || c == 's') return true;
        if(c == 'W' || c == 'w') return true;
        if(c == 'B' || c == 'b') return true;
        if(c == 'D' || c == 'd') return true;
        if(c == 'H' || c == 'h') return true;
        if(c == 'V' || c == 'v') return true;
        return false;
    }

/** ~~ removeSpacesDigits(String s) -> take a string like:
*     gcgcctcaat caactgttct gcagagatcc cattgtcaaa ggcatcaccc agccaacgct    188520
* return:
* gcgcctcaatcaactgttctgcagagatcccattgtcaaaggcatcacccagccaacgct
*/
    public String removeSpacesDigits(String s){
        String sp = "";
        if(valid(s)== true){
            for(int i=0; i<s.length(); i++){
                char c = s.charAt(i);
                if( valid(c) ){
                    sp += c;
                }
            }
        }
        return sp;
    }
    public char getStrand(){
        return sp.getStrand();
    }

    /*
     * Does exact matching (same strand) on na sequences to get the first position object for the subsequence relative to the contig
     */
    public SequencePosition naSubPosition(NASequence contig, NASequence substring){
     String c = contig.get();
     String s = substring.get();
     int start = c.indexOf(s);
     if(start == -1) return null;
     SequencePosition sp = new SequencePosition();
     sp.set(start+1, start+substring.size(), orient( contig.getStrand(), substring.getStrand() ) );
     return sp;
    }

    /*
     * Orient gives us the strand based on two sequences, s1 and s2:
     * s1 | s2 | orient
     * +  | +  | +
     * +  | -  | -
     * -  | +  | -
     * -  | -  | -
     */
    public char orient(char strand1, char strand2){
        if(strand1 == '+' && strand2 == '+'){
            return '+';
        }else return '-';
    }
}
